package org.sample.rep;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

public class BatchReplace {

    public static void main(String[] args) throws Exception {
        File dir = new File("E:/work/workspace/forum/src/main/java/com/youxiduo/module/forum/model/");

        String source = "@Table[(]name = \"[^\"]*\"[)]";
        String target = "";

        File[] listFiles = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                if (name.startsWith("BaseBO"))
                    return false;
                return name.endsWith(".java");
            }
        });

        for (File file : listFiles) {
            StringBuilder builder = new StringBuilder();
            String temp = null;

            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
            while ((temp = reader.readLine()) != null) {
                builder.append(temp);
                builder.append("\n");
            }
            reader.close();
            temp = builder.toString();
            temp = temp.replaceAll(source, target);

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8));
            writer.write(temp);
            writer.close();

        }
    }
}
